package com.example.project_dish.Dishes;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.project_dish.Order.OrderEvent;
import com.example.project_dish.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;


public class DishesDetailAdapter extends ArrayAdapter<DishesDetail> {
    private int resourceId;

    public DishesDetailAdapter(@NonNull Context context, int resource, @NonNull ArrayList<DishesDetail> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        //1.拿到数据
        DishesDetail item = getItem(position);

        View view; //自己创建的view对象，来替换

        ViewHolder viewHolder = new ViewHolder();


        //优化逻辑
        //1、如果这个水果（列表项）还没来到手机屏幕，按以前的的来（给它一个加载一个初始界面）
        if (convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);

            viewHolder.imageView = view.findViewById(R.id.dishes_image);

            viewHolder.textView = view.findViewById(R.id.dishes_detail);

            viewHolder.textView1 = view.findViewById(R.id.dishes_price);

            viewHolder.imageView1 = view.findViewById(R.id.add_select);

//            viewHolder.imageView2 = view.findViewById(R.id.reduce_select);

            view.setTag(viewHolder);    //存起来
        }else{
            //2、如果这个水果（列表项）以前来过，用以前的界面
            view = convertView;
            //不再次寻找,从ViewHolder里去拿出来
            viewHolder = (ViewHolder)view.getTag();
        }

        viewHolder.imageView.setImageResource(item.getImageId());

        viewHolder.textView.setText(item.getContent() + "");

        viewHolder.textView1.setText("价格为:" + item.getPrice() + "");

        viewHolder.imageView1.setImageResource(R.drawable.add);

//        viewHolder.imageView2.setImageResource(R.drawable.reduce);

        final View view1 = LayoutInflater.from(getContext()).inflate(R.layout.dishes_show, null);

        viewHolder.imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int imageId = item.getImageId();
                String name = item.getContent();
                int price = item.getPrice();
                Toast.makeText(getContext(), "您添加了：" + name + "于购物车中", Toast.LENGTH_SHORT).show();
                //把数据通过EventBus传给我的订单页
                String comment = "份量足，嚼劲足，口感好";
                EventBus.getDefault().post(new OrderEvent(comment,imageId,name,"￥"+price,1));
            }
        });

        //4.把界面丢回去展示
        return view;
    }

    class ViewHolder{
        ImageView imageView;
        TextView textView;
        TextView textView1;
        ImageView imageView1;
        ImageView imageView2;

    }

}
